
package com.ido.babyshow.util;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;

import sun.misc.BASE64Encoder;

import com.ido.babyshow.common.GlobalConstant;

/**
 * 工具类
 */
public class Utils
{
    private static Logger logger = LoggerFactory.getLogger(Utils.class);    
    
   
    public static String createFileName(String fileType) throws IOException{       
        long now=System.currentTimeMillis();
        return now+fileType;
    }
    
    
    
    /**
     * 上传文件到目标地址
     * @param uploadFile
     * @param url
     * @return
     * @throws IOException 
     */
    public static boolean uploadFile(File uploadFile,int motherId,String fileName) throws IOException{    
        String imageURL=GlobalConstant.UPLOAD_URL_PREFIX;       
        String directoryURL= imageURL+motherId+File.separator;
        File directory=new File(directoryURL);
        if(!directory.exists()){
          boolean createDirectory=  directory.mkdir(); 
          if(!createDirectory){
             throw new IOException("创建文件目录失败,"+createDirectory);
          }
        }
        File serverFile=new File(directoryURL,fileName);        
        try
        {
            serverFile.createNewFile();
            int readSize=FileCopyUtils.copy(uploadFile, serverFile);
            return readSize>0?true:false;
        }
        catch (IOException e)
        {
            logger.error("上传文件错误,上传文件为"+uploadFile.getAbsolutePath()+",服务器地址为"+serverFile.getAbsolutePath(),e);
            e.printStackTrace();
            return false;
        }
    }

    public static int convStrToInt(String value, int defaultValue)
    {
        if (StringUtils.isBlank(value))
        {
            return defaultValue;
        }
        try
        {
            int tempValue = Integer.parseInt(value);
            return tempValue;
        }
        catch (NumberFormatException ex)
        {
            ex.printStackTrace();
        }
        return defaultValue;

    }

    /**
     * base64对图片编码
     * 
     * @param photo
     * @return
     */
    public static String encodeImage(String image)
    {
        BASE64Encoder encoder = new BASE64Encoder();
        try
        {
            return String.valueOf(encoder.encode(image.getBytes("UTF-8")));
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * MD5加密
     * 
     * @param inputs
     * @return
     */
    public static String md5Update(String inputs)
    {
        try
        {
            byte[] inputBytes = inputs.getBytes("UTF-8");
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(inputBytes);
            return String.valueOf(inputBytes);
        }
        catch (NoSuchAlgorithmException e)
        {

            e.printStackTrace();
        }
        catch (UnsupportedEncodingException e)
        {

            e.printStackTrace();
        }
        return null;
    }

    /**
     * 对字符串特殊字符进行转义
     * 
     * @param inputs
     * @return
     */
    public static String escapeChar(String inputs)
    {
        if (StringUtils.isNotBlank(inputs))
        {
            inputs =
                inputs.replace("<", "&lt;").replace(">", "&gt;").replace(
                    String.valueOf(((char)13)), "<br/>").replace(
                    String.valueOf((char)32), "&nbsp;");
        }
        return inputs;
    }

   

    /**
     * MD5 加密方法
     */
    public static String MD5Encrypt(String inStr)
    {
        MessageDigest messageDigest = null;
        try
        {
            messageDigest =
                MessageDigest.getInstance(GlobalConstant.ENCRYPT_TYPE);
            messageDigest.reset();
            messageDigest.update(inStr.getBytes(GlobalConstant.SYS_CHAR_SET));
        }
        catch (NoSuchAlgorithmException e)
        {
            logger.error("[工具类]MD5加密失败", e);
            return null;
        }
        catch (UnsupportedEncodingException e)
        {
            logger.error("[工具类]字符集异常", e);
            return null;
        }
        byte[] byteArray = messageDigest.digest();

        StringBuffer md5StrBuff = new StringBuffer();

        for (int i = 0; i < byteArray.length; i++)
        {
            if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
                md5StrBuff.append("0").append(
                    Integer.toHexString(0xFF & byteArray[i]));
            else
                md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
        }
        return md5StrBuff.toString();
    }
}
